package com.ie.service.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.mapper.before.CartMapper;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartEntity> implements CartService {
    @Override
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<CartEntity>();
        queryWrapper.eq("busertable_id", cartEntity.getBusertableId());
        this.remove(queryWrapper);
        return ResponseResult.getMessageResult(null, "A001");
    }

    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        Long n = query()
                .eq("busertable_id", cartEntity.getBusertableId())
                .eq("goodstable_id", cartEntity.getGoodstableId())
                .count();
        boolean flag = false;
        if (n > 0) {

            UpdateWrapper<CartEntity> updateWrapper = new UpdateWrapper<>();
            //Preparing: UPDATE carttable SET shoppingnum = shoppingnum + 1 where busertable_id = 1 and goodstable_id = 32
            updateWrapper.setSql("shoppingnum=shoppingnum+"+cartEntity.getShoppingnum());
            updateWrapper.last("where busertable_id = " + cartEntity.getBusertableId() +
                    " and goodstable_id = " + cartEntity.getGoodstableId());
            flag = this.update(updateWrapper);
        } else {
            flag = save(cartEntity);
        }
        if (flag) {//
            return ResponseResult.getMessageResult(null, "A001");
        } else {//添加
            return ResponseResult.getMessageResult(null, "A002");
        }
    }
}
